/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aal.db;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 *
 * @author HUNG
 */
public class ConnectionFactory {
    public static Connection connectionFactory() throws Exception {        
        
        File file = new File("config.txt");
                StringBuilder contents = new StringBuilder();
                BufferedReader reader = null;
                int i=0;
                String host="";
                String prot="";
                String user="";
                String pass="";
                try {
                    reader = new BufferedReader(new FileReader(file));
                    String text = null;
                    // repeat until all lines is read
                    while ((text = reader.readLine()) != null) {
                        contents.append(text).append(System.getProperty("line.separator"));
                        i++;
                        if (i==1) {
                            host=text;
                        }
                        else if (i==2) {
                            prot =text;
                        }
                        else if (i==3) {
                            user=text;
                        }
                        else{
                            pass=text;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    try {
                        if (reader != null) {
                            reader.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

        Connection conn = null;
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        if (host.equalsIgnoreCase(null)) {
            conn =  DriverManager.getConnection("jdbc:sqlserver://" + "localhost" +":" + "1433" + "; databaseName = AAL", "sa", "1234567");
        }
        else
        {
        conn =  DriverManager.getConnection("jdbc:sqlserver://" + host +":" + prot + "; databaseName = AAL", user, pass);
        }
        return conn;
    }
    public static void closeConnection(ConnectionFactory conn) throws Exception{
        if (conn!=null) {
            conn.clone();
        }
    }
}
